import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index'

Vue.use(Router)


function urlDel(name) {
    var loca = window.location;
    //					   console.log(loca)
    var baseUrl = loca.origin + loca.hash;
    //					     console.log(baseUrl)
    return baseUrl
}

const Vuerouter = new Router({
    routes: [
        {
            path: '/',
            name: 'index',
            component: index,
            meta: {
                requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        }
    ]
})



Vuerouter.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {// 判断该路由是否需要登录权限
        var loca = window.location;
        //     	console.log(loca)
        if (loca.search.indexOf('token') == -1) {  // 获取当前url的token是否存在
            next();
        } else {
            let token = loca.search.substr(loca.search.indexOf('token'), loca.search.length)
            sessionStorage.setItem('token', token.split('=')[1]);
            //      	window.location.replace(urlDel('token'))
            history.pushState(null, null, urlDel('token'));
            next();
        }
    }
    else {
        next();
    }
})
export default Vuerouter;